The initialization of the sys.path module search path
The first entry in the module search path is the directory that contains the input script, if there is one.
入力スクリプトを含むディレクトリ
Otherwise, the first entry is the current directory, which is the case when executing the interactive shell, a -c command, or -m module.
入力スクリプトを含むディレクトリがなければカレントディレクトリ
The PYTHONPATH environment variable is often used to add directories to the search path. The next items added are the directories containing standard Python modules as well as any extension modules that these modules depend on.
.pydや.so
The directory with the platform-independent Python modules is called prefix. The directory with the extension modules is called exec_prefix.
The PYTHONHOME environment variable may be used to set the prefix and exec_prefix locations.
積ん読
Once found, prefix and exec_prefix are available at sys.prefix and sys.exec_prefix respectively.
Finally, the site module is processed and site-packages directories are added to the module search path.
IMO:site-packagesがsys.pathに入るから、pip installしたパッケージをimportできるんだな〜
仮想環境 (Virtual environments)
If Python is run in a virtual environment (略) then prefix and exec_prefix are specific to the virtual environment.
pyvenv.cfgがあるときの動きが書いてある
If a pyvenv.cfg file is found alongside the main executable, or in the directory one level above the executable, the following variations apply:
If home is an absolute path and PYTHONHOME is not set, this path is used instead of the path to the main executable when deducing prefix and exec_prefix.
_pth ファイル
To completely override sys.path create a ._pth file with the same name as the shared library or executable (python._pth or python311._pth).
In the ._pth file specify one line for each path to add to sys.path.
import site を指定したときは、(アンダースコアが前に付かない) .pth ファイルは site モジュールにより通常通り処理されることに注意してください。
Embedded Python
prefixとexec_prefix